<template>
  <div>
    <el-button
      style="float: left; margin-left: 15px"
      icon="el-icon-plus"
      type="primary"
      plain
      size="small"
      @click="show"
      >新增</el-button
    ><el-button
      style="float: left; margin-left: 15px"
      type="danger"
      plain
      icon="el-icon-delete"
      size="small"
      @click="delAll"
      :disabled="this.multipleSelection.length == 0"
      >批量删除</el-button
    >
    <el-input
      style="width: 240px; float: left; margin-left: 20px"
      size="small"
      placeholder="请输入分类ID"
      v-model="id"
    />
    <el-input
      style="width: 240px; float: left; margin-left: 20px"
      size="small"
      placeholder="请输入分类名称"
      v-model="typeName"
    />
    <el-input
      style="width: 240px; float: left; margin-left: 20px"
      size="small"
      placeholder="请输入父ID"
      v-model="parentId"
    />
    <el-button
      type="primary"
      icon="el-icon-search"
      style="float: left; margin-left: 20px"
      size="small"
      @click="initType"
      >搜索</el-button
    >
    <el-button
      style="float: left; margin-left: 15px; margin-bottom: 12px"
      icon="el-icon-refresh"
      size="small"
      @click="clearInput"
      >重置</el-button
    >
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%; margin-top: 10px"
      :cell-style="rowClass"
      :header-cell-style="headClass"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="45"> </el-table-column>
      <el-table-column prop="id" label="ID" width="200"></el-table-column>
      <el-table-column
        prop="typeName"
        label="分类名称"
        width="400"
      ></el-table-column>
      <el-table-column prop="parentId" label="父ID" width="200">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 1.5%; float: right">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="currentPage"
        :page-sizes="[5, 8]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      title="添加分类"
      :visible.sync="dialogVisible"
      width="400px"
      center
    >
      <el-form
        :model="formData"
        ref="registerForm"
        label-width="80px"
        :rules="rules"
      >
        <el-form-item prop="typeName" label="分类名称">
          <el-input
            v-model="formData.typeName"
            placeholder="分类名称"
          ></el-input>
        </el-form-item>
        <el-form-item prop="parentId" label="父分类">
          <el-select
            style="width: 270px"
            filterable
            v-model="formData.parentId"
            clearable
            placeholder="请选择父分类(输入关键字进行搜索)"
          >
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.typeName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button size="small" @click="dialogVisible = false">取消</el-button>
        <el-button size="small" type="primary" @click="addType">确定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="编辑分类"
      :visible.sync="editDialogVisible"
      width="400px"
      center
    >
      <el-form
        :model="editData"
        ref="editForm"
        label-width="80px"
        :rules="rules"
      >
        <el-form-item prop="typeName" label="分类名称">
          <el-input
            :disabled="true"
            v-model="editData.typeName"
            placeholder="分类名称"
          ></el-input>
        </el-form-item>
        <el-form-item prop="parentId" label="父分类">
          <el-select
            style="width: 270px"
            filterable
            v-model="editData.parentId"
            clearable
            placeholder="请选择父分类(输入关键字进行搜索)"
          >
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.typeName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button size="mini" @click="editDialogVisible = false"
          >取消</el-button
        >
        <el-button size="mini" type="primary" @click="updateType"
          >确定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogVisible: false,
      editDialogVisible: false,
      tableData: [],
      currentPage: 1,
      pageSize: 8,
      total: 0,
      id: "",
      typeName: "",
      parentId: "",
      options: [],
      multipleSelection: [],
      formData: {
        parentId: "",
        typeName: "",
      },
      editData: {
        id: "",
        parentId: "",
        typeName: "",
      },
      rules: {
        typeName: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          {
            min: 2,
            max: 250,
            message: "长度在 2 到 250 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.initType();
  },
  methods: {
    show() {
      this.formData = {};
      this.dialogVisible = true;
      this.initParentType();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.initType();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.initType();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    rowClass() {
      return "text-align: center;";
    },
    headClass() {
      return "text-align: center;background:#f8f8f9;";
    },
    clearInput() {
      location.reload();
    },
    handleEdit(row) {
      this.editData.id = row.id;
      this.editData.typeName = row.typeName;
      this.editData.parentId = row.parentId;
      this.editData.isParent = row.isParent;
      this.initParentType();
      this.editDialogVisible = true;
    },
    updateType() {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          this.$put("type/", this.editData).then((res) => {
            if (res.code == 200) {
              this.initType();
              this.editDialogVisible = false;
            }
          });
        } else {
          this.$message.error("数据校验失败，请检查");
        }
      });
    },
    handleDelete(row) {
      this.$confirm(
        '此操作将删除ID为"' + row.id + '"的数据, 是否继续?',
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        if (!row.isParent) {
          this.$delete("type/?ids=" + row.id).then((res) => {
            if (res.code == 200) {
              this.initType();
            }
          });
        } else {
          this.$message.error("该分类为父分类或其下有岗位，请先清除关联数据");
        }
      });
    },
    addType() {
      this.$refs["registerForm"].validate((valid) => {
        if (valid) {
          this.$post("type/", this.formData).then((res) => {
            if (res.code == 200) {
              this.initType();
              this.dialogVisible = false;
            }
          });
        } else {
          this.$message.error("数据校验失败，请检查");
        }
      });
    },
    initParentType() {
      this.$get("type/").then((res) => {
        if (res.code == 200 && res.data) {
          this.options = res.data;
        }
      });
    },
    initType() {
      this.$get(
        "type/" +
          this.currentPage +
          "/" +
          this.pageSize +
          "?id=" +
          this.id +
          "&typeName=" +
          this.typeName +
          "&parentId=" +
          this.parentId
      ).then((res) => {
        if (res.code == 200 && res.code) {
          this.tableData = res.data.records;
          this.total = res.data.total;
        }
      });
    },
    delAll() {
      this.$confirm(
        '此操作将删除"' + this.multipleSelection.length + '"个分类, 是否继续?',
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          try {
            let ids = "?";
            this.multipleSelection.forEach((item) => {
              if (item.isParent) {
                this.$message.error(
                  "所选数据有父分类或其下有岗位，请先清除关联数据"
                );
                throw Error();
              } else {
                ids += "ids=" + item.id + "&";
              }
            });
            this.$delete("type/" + ids).then((res) => {
              if (res.code == 200) {
                this.initType();
              }
            });
          } catch (err) {
            console.log(err);
          }
        })
        .catch(() => {
          this.initType();
        });
    },
  },
};
</script>

<style>
</style>